package com.genius.mobile_page.pandian_jd;

import java.sql.SQLException;

import com.alibaba.fastjson.JSONObject;
import com.genius.dingding.util.GeniusConfig;
import com.genius.dingding.util.HttpHelper;
import com.genius.dingding.util.MessageHelper;
import com.genius.mobile_page.common.ErrorResponseData;
import com.genius.mobile_page.common.ResponseData;
import com.genius.mobile_page.common.SuccessResponseData;
import com.genius.mobile_page.pingjia.PingjiaUtil;
import com.genius.workflow.util.DBTools;
import com.genius.workflow.util.DBTools2;

import cn.hutool.core.util.ObjectUtil;

public class pandianjdUtil {
	//发送确认通知
	public static Long sendQuerenTongzhi(String querenUser,String pandianid,String shopcode,String shopname){
		long agentId = GeniusConfig.JINDUPANDIANAGENTID;
		String messageUrl = GeniusConfig.PROJECTPATH+"/mobile_page/pandian_jd/toJiancha_jieguo_detail"
				+ "?pandianid="+pandianid+"&corp=jindushenghuo&type=queren&v="+System.currentTimeMillis();
		String picUrl = GeniusConfig.PROJECTPATH+"/genius/images/dingding/gdxt_pt.png";
		String title = "今度盘点检查确认通知";
		String accessToken = HttpHelper.httpGetString(GeniusConfig.JINDUSHENGHUO+"/dingding/gettoken.do");
		if(ObjectUtil.isEmpty(shopname)){
			JSONObject pJson = getPandian_jiancha(pandianid);
			shopcode = pJson.getString("shopcode");
			shopname = pJson.getString("shopname");
			pJson = null;
		}
		String text = shopname+"("+shopcode+")盘点检查完成，需要您的确认，点击进行确认";
		querenUser = "134916456822004904";
		Long task = null;
		try {
			task = MessageHelper.createLink(agentId, querenUser, null, messageUrl, picUrl, title, text, accessToken);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return task;
	}
	//发送回复通知
	public static Long sendHuifuTongzhi(String sendUser,String pandianid,String pandianUser,String shopcode,String shopname){
		long agentId = GeniusConfig.JINDUPANDIANAGENTID;
		String messageUrl = GeniusConfig.PROJECTPATH+"/mobile_page/pandian_jd/toJiancha_jieguo_detail"
				+ "?pandianid="+pandianid+"&corp=jindushenghuo&type=huifu&v="+System.currentTimeMillis();
		String picUrl = GeniusConfig.PROJECTPATH+"/genius/images/dingding/gdxt_pt.png";
		String title = "今度盘点检查回复通知";
		String accessToken = HttpHelper.httpGetString(GeniusConfig.JINDUSHENGHUO+"/dingding/gettoken.do");
		if(ObjectUtil.isEmpty(shopname)){
			JSONObject pJson = getPandian_jiancha(pandianid);
			shopcode = pJson.getString("shopcode");
			shopname = pJson.getString("shopname");
			pJson = null;
		}
		String text = shopname+"("+shopcode+")盘点检查完成，需要您的回复，点击进行回复";
//		sendUser += ",014136441221947361";
		Long task = null;
		try {
			task = MessageHelper.createLink(agentId, sendUser, null, messageUrl, picUrl, title, text, accessToken);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return task;
	}

	//发送门店通知
	public static Long sendShopTongzhi(String sendUser,String pandianid,String pandianUser,String shopcode,String shopname){
		long agentId = GeniusConfig.JINDUPANDIANAGENTID;
		String messageUrl = GeniusConfig.PROJECTPATH+"/mobile_page/pandian_jd/toJiancha_jieguo_detail"
				+ "?pandianid="+pandianid+"&corp=jindushenghuo&type=chakan&v="+System.currentTimeMillis();
		String picUrl = GeniusConfig.PROJECTPATH+"/genius/images/dingding/gdxt_pt.png";
		String title = "今度盘点检查通知";
		String accessToken = HttpHelper.httpGetString(GeniusConfig.JINDUSHENGHUO+"/dingding/gettoken.do");
		if(ObjectUtil.isEmpty(shopname)){
			JSONObject pJson = getPandian_jiancha(pandianid);
			shopcode = pJson.getString("shopcode");
			shopname = pJson.getString("shopname");
			pJson = null;
		}
		String text = shopname+"("+shopcode+")盘点检查完成，点击查看";
//		sendUser += ",134916456822004904";
		Long task = null;
		try {
			task = MessageHelper.createLink(agentId, sendUser, null, messageUrl, picUrl, title, text, accessToken);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return task;
	}
	//发送督导通知
	public static Long sendDudaoTongzhi(String sendUser,String pandianid,String shopcode,String shopname){
		long agentId = GeniusConfig.JINDUPANDIANAGENTID;
		String messageUrl = GeniusConfig.PROJECTPATH+"/mobile_page/pandian_jd/toJiancha_jieguo_detail"
				+ "?pandianid="+pandianid+"&corp=jindushenghuo&type=zongjie&v="+System.currentTimeMillis();
		String picUrl = GeniusConfig.PROJECTPATH+"/genius/images/dingding/gdxt_pt.png";
		String title = "今度盘点总结报告录入通知";
		String accessToken = HttpHelper.httpGetString(GeniusConfig.JINDUSHENGHUO+"/dingding/gettoken.do");
		if(ObjectUtil.isEmpty(shopname)){
			JSONObject pJson = getPandian_jiancha(pandianid);
			shopcode = pJson.getString("shopcode");
			shopname = pJson.getString("shopname");
			pJson = null;
		}
		String text = shopname+"("+shopcode+")盘点检查完成，需要您录入盘点总结报告，点击进行录入";
//		sendUser += ",014136441221947361,134916456822004904";
		Long task = null;
		try {
			task = MessageHelper.createLink(agentId, sendUser, null, messageUrl, picUrl, title, text, accessToken);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return task;
	}
	//发送反馈通知
	public static Long sendFankuiTongzhi(String huifuUser,String pandianid,String shopcode,String shopname){
		long agentId = GeniusConfig.JINDUPANDIANAGENTID;
		String messageUrl = GeniusConfig.PROJECTPATH+"/mobile_page/pandian_jd/toJiancha_wentifankui"
				+ "?pandianid="+pandianid+"&shopcode="+shopcode+"&corp=jindushenghuo&type=huifu&v="+System.currentTimeMillis();
		String picUrl = GeniusConfig.PROJECTPATH+"/genius/images/dingding/gdxt_pt.png";
		String title = "今度盘点反馈问题回复通知";
		String accessToken = HttpHelper.httpGetString(GeniusConfig.JINDUSHENGHUO+"/dingding/gettoken.do");
		if(ObjectUtil.isEmpty(shopname)){
			JSONObject pJson = getPandian_jiancha(pandianid);
			shopcode = pJson.getString("shopcode");
			shopname = pJson.getString("shopname");
			pJson = null;
		}
		String text = shopname+"("+shopcode+")盘点检查完成，需要您进行回复，点击进行回复";
//		huifuUser += ",014136441221947361,134916456822004904";
		Long task = null;
		try {
			task = MessageHelper.createLink(agentId, huifuUser, null, messageUrl, picUrl, title, text, accessToken);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return task;
	}
	//发送反馈完成通知
	public static Long sendFankuiWanchengTongzhi(String wanchengUser,String pandianid,String shopcode,String shopname){
		long agentId = GeniusConfig.PANDIANAGENTID;
		String messageUrl = GeniusConfig.PROJECTPATH+"/mobile_page/pandian_jd/toJiancha_wentifankui"
				+ "?pandianid="+pandianid+"&shopcode="+shopcode+"&corp=jindushenghuo&type=wancheng&v="+System.currentTimeMillis();
		String picUrl = GeniusConfig.PROJECTPATH+"/genius/images/dingding/gdxt_pt.png";
		String title = "盘点反馈问题完成通知";
		String accessToken = HttpHelper.httpGetString(GeniusConfig.QIYEGONGZUO+"/dingding/gettoken.do");
		if(ObjectUtil.isEmpty(shopname)){
			JSONObject pJson = getPandian_jiancha(pandianid);
			shopcode = pJson.getString("shopcode");
			shopname = pJson.getString("shopname");
			pJson = null;
		}
		String text = shopname+"("+shopcode+")盘点反馈问题已经回复，需要您完成，点击进行完成";
//				huifuUser += ",014136441221947361,134916456822004904";
		Long task = null;
		try {
			task = MessageHelper.createLink(agentId, wanchengUser, null, messageUrl, picUrl, title, text, accessToken);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return task;
	}
	//插入回复人信息
	public static ResponseData insertPandianJianchaWentifankui(String level, String pandianid) {
		try {
			
			JSONObject sendShopUserJson = DBTools2.select("select GROUP_CONCAT(id)shopusers from "+GeniusConfig.JINDUZHIBIAODATABASE+".s_userinfo"
					+ " where FIND_IN_SET((SELECT shopcode FROM pandian_jiancha_jd where id = "+pandianid+"),jobnumber)");
			
			String huifuuserjob,huifuuserjobname,sendUserjob,sendUserjobSql;
			if(ObjectUtil.equals("1", level)){
				huifuuserjob = "Employee";
				huifuuserjobname = "督导";
				sendUserjob = "sEmployeeNO,',',sZoneMagNO";
				sendUserjobSql = "left(sEmployeeNO,6),',',left(sZoneMagNO,6)";
			}else if(ObjectUtil.equals("2", level)){
				huifuuserjob = "ZoneMag";
				huifuuserjobname = "区总";
				sendUserjob = "sEmployeeNO,',',sZoneMagNO,',',sDirectorNO";
				sendUserjobSql = "left(sEmployeeNO,6),',',left(sZoneMagNO,6),',',left(sDirectorNO,6)";
			}else{
				huifuuserjob = "Director";
				huifuuserjobname = "总监";
				sendUserjob = "sEmployeeNO,',',sZoneMagNO,',',sDirectorNO";
				sendUserjobSql = "left(sEmployeeNO,6),',',left(sZoneMagNO,6),',',left(sDirectorNO,6)";
			}
			JSONObject sendUserJson = DBTools2.select("select GROUP_CONCAT(id)users from "+GeniusConfig.JINDUZHIBIAODATABASE+".s_userinfo"
					+ " where FIND_IN_SET(jobnumber,(SELECT CONCAT("+sendUserjobSql+")"
					+ " FROM pandian_jiancha_jd where id = "+pandianid+"))");
			JSONObject pandianJson = DBTools2.select("SELECT s1.*,s2.id huifuuser from"
					+" (SELECT id pandianid,shopcode,shopname,pandianUser,pandianUsername,`level`,sEmployeeNO zongjieuserjobnumber"
					+ ",sEmployeeName zongjieusername,left(s"+huifuuserjob+"NO,6) huifuuserjobnumber"
					+ ",s"+huifuuserjob+"Name huifuusername FROM pandian_jiancha_jd where id = "+pandianid+") s1"
					+" LEFT JOIN "
					+GeniusConfig.JINDUZHIBIAODATABASE+".s_userinfo s2 on s1.huifuuserjobnumber = s2.jobnumber");
			if(ObjectUtil.isEmpty(pandianJson.getString("level"))){
				pandianJson.put("level", level);
			}
			if(ObjectUtil.equals("3", level)){//最高等级添加总经理
				pandianJson.put("sendUser", sendUserJson.getString("users")+","+sendShopUserJson.getString("shopusers")+","+GeniusConfig.PANDIANZONGJINGLIUSER);
			}else{
				pandianJson.put("sendUser", sendUserJson.getString("users")+","+sendShopUserJson.getString("shopusers"));
			}
			//发送门店通知
			pandianjdUtil.sendShopTongzhi(sendShopUserJson.getString("shopusers"), pandianid
					, pandianJson.getString("pandianUsername"), pandianJson.getString("shopcode"), pandianJson.getString("shopname"));
			//发送回复通知
			Long task = pandianjdUtil.sendHuifuTongzhi(sendUserJson.getString("users"), pandianid
					, pandianJson.getString("pandianUsername"), pandianJson.getString("shopcode"), pandianJson.getString("shopname"));
			if(ObjectUtil.isEmpty(task)){
				task = 0L;
			}
			pandianJson.put("sendTask", task);
			pandianJson.put("huifuuserjob", huifuuserjob);
			pandianJson.put("huifuuserjobname", huifuuserjobname);
			//发送总结通知
			JSONObject sendZongjieUserJson = DBTools2.select("select GROUP_CONCAT(id)users from "+GeniusConfig.JINDUZHIBIAODATABASE+".s_userinfo"
					+ " where FIND_IN_SET((SELECT sEmployeeNO FROM pandian_jiancha_jd where id = "+pandianid+"),jobnumber)");
			
			Long zongjietask = pandianjdUtil.sendDudaoTongzhi(sendZongjieUserJson.getString("users"), pandianid
					, pandianJson.getString("shopcode"), pandianJson.getString("shopname"));
			if(ObjectUtil.isEmpty(zongjietask)){
				zongjietask = 0L;
			}
			pandianJson.put("zongjietask", zongjietask);
			pandianJson.put("zongjieuser", sendZongjieUserJson.getString("users"));
//			System.out.println(pandianJson);
			DBTools.replace("pandianid,sendUser,sendTask,sendDate=now(),huifuFlag=0,huifuuser,huifuusername"
					+ ",huifuuserjobnumber,huifuuserjob,huifuuserjobname,zongjieuser,zongjieTask,zongjieSendDate=now()"
					+ ",zongjieFlag=0,zongjieusername,zongjieuserjobnumber", "pandian_jiancha_huifu_jd", pandianJson);
//			DBTools2.update("sendUser,sendTask,sendDate=now(),huifuFlag=0,huifuuser,huifuusername"
//					+ ",huifuuserjobnumber,huifuuserjob,huifuuserjobname", "pandianid", "pandian_jiancha_huifu_jd", pandianJson);
			return new SuccessResponseData();
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点回复新增失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}
	//插入总结人信息
	public static ResponseData insertPandianJianchaZongjie(String pandianid) {
		try {
			
			JSONObject sendUserJson = DBTools2.select("select GROUP_CONCAT(id)users from "+GeniusConfig.JINDUZHIBIAODATABASE+".s_userinfo"
					+ " where FIND_IN_SET((SELECT sEmployeeNO FROM pandian_jiancha_jd where id = "+pandianid+"),jobnumber)");
			
			
			JSONObject pandianJson = DBTools2.select("SELECT s1.*,s2.id zongjieuser from"
					+" (SELECT id pandianid,sEmployeeNO zongjieuserjobnumber,sEmployeeName zongjieusername FROM pandian_jiancha_jd where id = "+pandianid+") s1"
					+" LEFT JOIN "
					+GeniusConfig.JINDUZHIBIAODATABASE+".s_userinfo s2 on s1.zongjieuserjobnumber = s2.jobnumber");
			
			
			//发送总结通知
			Long task = pandianjdUtil.sendDudaoTongzhi(sendUserJson.getString("users"), pandianid
					, pandianJson.getString("shopcode"), pandianJson.getString("shopname"));
			if(ObjectUtil.isEmpty(task)){
				task = 0L;
			}
			pandianJson.put("zongjieTask", task);
			DBTools2.insert("pandianid,zongjieuser,zongjieTask,zongjieSendDate=now(),zongjieFlag=0,zongjieusername"
					+ ",zongjieuserjobnumber", "pandian_jiancha_huifu_jd", pandianJson);
			return new SuccessResponseData();
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点回复新增失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}
	
	//插入总结人信息
		public static JSONObject getPandian_jiancha(String id) {
			JSONObject res = null;
			try {
				
				res = DBTools2.select("select * from pandian_jiancha_jd where id = ?", new String[]{id});
				
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			return res;
		}
		//插入总结人信息
		public static ResponseData changePandianDaipingjia(JSONObject parms) {
			try {
				JSONObject panduanJson = DBTools2.select("select s1.* , s2.* from "
							+"(SELECT COUNT(id)allnum,sum(IF(flag=1 or flag =2,1,0))wentihuifunum "
							+ " from pandian_jiancha_wentifankui_jd WHERE pandianid = ?) s1"
							+",(select COUNT(pandianid)huifunum from pandian_jiancha_huifu_jd "
							+ " WHERE pandianid = ? and huifuFlag = 1) s2"
							, new String[]{parms.getString("pandianid"),parms.getString("pandianid")});
				//盘点已回复并且所有问题都已回复
				if(panduanJson.getInteger("huifunum")> 0 && (panduanJson.getInteger("allnum")==0 
						|| (panduanJson.getInteger("allnum")> 0 && panduanJson.getInteger("allnum")== panduanJson.getInteger("wentihuifunum")))){
//					System.out.println("更改状态");
					//更改盘点状态
					parms.put("id", parms.getString("pandianid"));
					parms.put("flag", 7);
					DBTools2.update("flag", "id", "pandian_jiancha_jd", parms);
					
					parms.put("pingjiaType", "4");
//				System.out.println("parms->"+parms);
					//插入待评价信息
					PingjiaUtil.insertTypePingjia(parms);
				}
				return new SuccessResponseData();
				
				
			} catch (SQLException throwables) {
				
				throwables.printStackTrace();
				return new ErrorResponseData("盘点回复新增失败");
			} catch (Exception e) {
				e.printStackTrace();
				return new ErrorResponseData(e.getMessage());
			}
		}
	
}
